﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：StaffAdd.ascx.cs
//
//文件功能描述：员工添加
// 
//创建标识：2007.11.21 曹骞
//创建描述：
//
//修改标识：2007.11.28 曹骞
//修改描述：修正模式对话框显示的问题
//
//修改标识：2007.11.30 曹骞
//修改描述：修改页面传参问题，使用Server.Transfer传参
//
------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;
using System.Text;
using System.Threading;


/// <summary>
/// 员工添加及员工信息修改
/// </summary>
public partial class UserControls_StaffAdd : System.Web.UI.UserControl
{
    
    //创建一个临时label放置员工帐号
  static Label lblAcount = new Label();
    //创建一个临时label放置员工ID
  static Label lblStaffID = new Label();
    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
       
        
        if (!IsPostBack)
        {

            lblAcount.Text = string.Empty;
            lblStaffID.Text = string.Empty;
            //获取员工账号
            string strAccount = String.Empty;
            StaffService Service=new StaffService () ;
            if (Page.PreviousPage != null)
            {
                UserControl uc = (UserControl)Page.PreviousPage.FindControl("StaffManage1");
                lblAcount = (Label)uc.FindControl("lblAcount");
                lblStaffID = (Label)uc.FindControl("lblStaffID");
            }

            if (lblAcount.Text != null)
            {
                strAccount = lblAcount.Text;
            }         
            //由判断结果动态改变页标题
            if (strAccount==String.Empty)
            {
                //strAccount为空
                lnkOrderManage.Text = "员工添加";
                this.lblRole.Visible = true;
                this.lblStatus.Visible = true;
                this.lblPwd.Visible = true ;
                this.lblConfirm.Visible = true;
                this.txtPwd.Visible = true;
                this.txtConfirmPwd.Visible = true;
  
            }
            else if (strAccount != String.Empty)
            {
                //strAccount不为空
                lnkOrderManage.Text = "员工修改";
                this.lblRole.Visible = true;
                this.lblStatus.Visible = true;
                this.lblPwd.Visible = false;
                this.lblConfirm.Visible = false;
                this.txtPwd.Visible = false;
                this.txtConfirmPwd.Visible = false;
                this.rblUserRole.Visible = true  ;
                this.rblUserStatus.Visible = true ;
                this.valrPWD.Visible = false;
                this.valcPwd.Visible = false ;
                this.valrConfirmPwd.Visible = false;

                if (strAccount == "Admin")
                {
                    this.rblUserRole.Visible = false;
                    this.rblUserStatus.Visible = false;
                    this.lblRole.Visible = false;
                    this.lblStatus.Visible = false;
                    
                }

                    //由判断结果动态加载按钮的图片
                    this.ibtnAddStaff.ImageUrl = "~/Images/Smart/btn/btn_save.png";
                    string strStaffAccount = lblAcount.Text;
                    //实例化一个员工信息实体集
                    Staff staffInfo = Service.Select(strStaffAccount);
                    //将取得的值绑定到页面的各个TextBox
                    this.txtAccount.Text = staffInfo.Account;
                    this.txtFirstName.Text = staffInfo.FirstName;
                    this.txtLastName.Text = staffInfo.LastName;   
                    this.rblUserRole.SelectedValue = Convert.ToString (staffInfo.RoleID);
                  
                    
                    //判断员工的状态
                    if (staffInfo.Status)
                    {
                        //启用
                        this.rblUserStatus.SelectedValue = "1";
                    }
                    else
                    {
                        //禁用
                        this.rblUserStatus.SelectedValue = "0";
                    }
                }
       
            }
        }

    /// <summary>
    /// 撤销修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgbtnCancel_Click(object sender, ImageClickEventArgs e)
    {
        this.ValidateEnable(false);
        //跳转到员工管理页面
        Response.Redirect("~/Admin/StaffManage.aspx");
       //Server.Transfer("~/Admin/StaffManage.aspx");
       
    }

    /// <summary>
    /// 员工的添加或修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnAddStaff_Click(object sender, ImageClickEventArgs e)
    {   
        StaffService Service = new StaffService();

        int iRoleID = Convert.ToInt32(this.rblUserRole.SelectedValue);//获取角色ID
        int iRoleStatus = Convert.ToInt32(this.rblUserStatus.SelectedValue); //获取角色状态（启用还是禁用）
        string strAccount = this.txtAccount.Text.Trim();//获取员工帐号
        string strFirstName = this.txtFirstName.Text.Trim();//获取员工姓
        string strLastName = this.txtLastName.Text.Trim();//获取员工名
        string strAccountUpdate = String.Empty;

        if (lblAcount.Text !=null)
        {
            //若不为空则作更新处理
            strAccountUpdate = lblAcount .Text.Trim();
        }
        if (strAccountUpdate ==String.Empty)
        {
                string strPassword=this.txtPwd .Text.Trim () ;
                string strConfirm=this.txtConfirmPwd .Text.Trim() ;
                 byte[] Encode=new byte[20];
                Encode= Encoding(strPassword);
                //异步调用添加新员工方法 
                Service.AddCompleted += new AddCompletedEventHandler1(staffAdd_AddCompleted);
                Service.AddAsync(strAccount, strFirstName, strLastName, Encode, iRoleID, iRoleStatus);
            
        }
        else if (strAccountUpdate != String.Empty) 
        {
           
            Staff staff = Service.Select(strAccountUpdate);
            //获取员工编号
            int iStaffID = Convert.ToInt32(lblStaffID.Text.Trim());
            //异步调用方法修改员工信息
            Service.UpdateCompleted += new UpdateCompletedEventHandler(staffAdd_UpdateCompleted);
            Service.UpdateAsync(iStaffID, strAccount, strFirstName, strLastName, staff.Password, iRoleID, iRoleStatus);
          
        }

    }

    /// <summary>
    /// 获取异步调用结果
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void staffAdd_UpdateCompleted(object sender, UpdateCompletedEventArgs e)
    {
        // 提示用户操作结果
        if (e.Result)
        { 
          // Page.RegisterStartupScript("returns", "<script> confirm('修改成功,要返回到管理页面吗？');</script>"); 
           // Page.RegisterStartupScript("","<script> if (confirm('修改成功,要返回到管理页面吗？')==true){Location.href=~/StaffManage.aspx;}</script>");
            Page.RegisterStartupScript("warnings", "<script>alert('修改成功!');</script>");
            
            //Server.Transfer("~/StaffManage.aspx");
        } 

        else
        {
            Page.RegisterStartupScript("warnings", "<script>alert('修改失败');</script>");
        }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void staffAdd_AddCompleted(object sender, localhost.AddCompletedEventArgs1 e)
    {
        if (e.Result)
        {
            //提示信息
            Page.RegisterStartupScript("warnings", "<script>alert('添加成功');</script>");
   
        }
        else
        {
            //提示信息
            Page.RegisterStartupScript("warnings", "<script>alert('添加失败');</script>");
        }             
    }

    /// <summary>
    /// hash值转换算法
    /// </summary>
    /// <param name="chars">明文密码字符串</param>
    /// <returns>二进制hash值</returns>
    private byte[] Encoding(string chars)
    {
        //采用了SHA1算法计算输入密码的hash值
        SHA1Managed mydata = new SHA1Managed();
        byte[] bites = new byte[20];
        ASCIIEncoding pw = new ASCIIEncoding();
        bites = pw.GetBytes(chars);
        bites = mydata.ComputeHash(bites);
        return bites;
    }

    /// <summary>
    /// 验证控件启用开关
    /// </summary>
    /// <param name="validate"></param>
    void ValidateEnable(bool validate)
    {
        this.valcPwd.Visible  = validate;
        this.valrAccount.Visible = validate;
        this.valrConfirmPwd.Visible = validate;
        this.valrFirstName.Visible = validate;
        this.valrLastName.Visible = validate;
        this.valrPWD.Visible = validate;
    }
    
}
